package nz.ac.massey.doi_information_extractor.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import nz.ac.massey.doi_information_extractor.data.Publication;
import nz.ac.massey.doi_information_extractor.database.DatabaseInterface;

/**
 * Dialog to open a publication that has been stored in the database
 *
 */
@SuppressWarnings("serial")
public class OpenPublicationDialog extends JDialog implements ActionListener, ListSelectionListener{

    private static OpenPublicationDialog dialog;
    private JList list;
    private JTextArea previewArea;
    private JButton openButton, closeButton;
    private static Publication selectedPublication = null;
    private DatabaseInterface database;

    /**
     * Static Publication to show the dialog and return the selected publication after the dialog
     * has been closed
     *
	 * @param frameComp The frame the Dialog depends on
	 * @param locationComp The component to determine the dialogs location from
	 * @param database The database being used
     * @return
     */
	public static Publication showDialog(Component frameComp, DatabaseInterface database){
		Frame frame = JOptionPane.getFrameForComponent(frameComp);

		dialog = new OpenPublicationDialog(frame, database);

		dialog.setVisible(true);

		return selectedPublication;
	}


	/**
	 * Constructor for Open Publication Dialog
	 *
	 * @param frame The frame the Dialog depends on
	 * @param locationComp The component to determine the dialogs location from
	 * @param database The database being used
	 */
	private OpenPublicationDialog(Frame frame, DatabaseInterface database){

		super(frame, "Open Publication", true);

		this.database = database;

		init();

		list.setSelectedIndex(0);
	}

	/**
	 * Initialising the dialog box
	 */
	private void init(){
		this.setResizable(false);
        JPanel mainPanel = new JPanel();

        String[] doiList = null;
		try {
			doiList = database.getDoiList();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        if(doiList != null)
        	list = new JList(doiList);
        else
        	list = new JList();

		list.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		list.addListSelectionListener(this);

        JScrollPane listScroller = new JScrollPane(list);

        openButton = new JButton();
        openButton.setText("Open");
        openButton.addActionListener(this);

        closeButton = new JButton();
        closeButton.setText("Cancel");
        closeButton.addActionListener(this);

        previewArea = new JTextArea();
        previewArea.setColumns(20);
        previewArea.setRows(5);
        previewArea.setEditable(false);
        previewArea.setEnabled(false);
        previewArea.setDisabledTextColor(Color.BLACK);
        previewArea.setBackground(new Color(220,220,220));
        previewArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        previewArea.setFont(new Font("sansserif", Font.BOLD, 11));
        previewArea.setLineWrap(true);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listScroller, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(openButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(previewArea, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(previewArea, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(openButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(listScroller, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        getContentPane().add(mainPanel);

        pack();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == closeButton)
			selectedPublication = null;
		dialog.setVisible(false);
	}


	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		try {
			selectedPublication = database.read((String) list.getSelectedValue());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String displayString = "Publication\n";

		displayString += "Title: " + selectedPublication.getArticleTitle() + "\n\n";
		displayString += "Doi: " + selectedPublication.getDoi() + "\n";
		displayString += "Publication Type: " + selectedPublication.getPublicationType() + "\n";
		displayString += "Authors: " + selectedPublication.getAuthors().getAuthorsString() + "\n";

		previewArea.setText(displayString);
	}
}
